/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workload;

import java.sql.DriverManager;
import commons.Constants;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class CloudMonitorWorkload {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // parameters

        long experiment_duration = 20; // in minutes
        int[] db_id_list = {1, 2, 3, 5, 8, 13}; // list of monitored db

        // program
        try {
            Random random = new Random();
            Class.forName(Constants.DB_DRIVER);
            Connection connection = DriverManager.getConnection(Constants.DB_ADDRESS, Constants.DB_USERNAME, Constants.DB_PASSWORD);
            System.out.println("DB Connected");
            experiment_duration = System.currentTimeMillis() + experiment_duration * 60 * 1000;
            long experiment_time = System.currentTimeMillis();
            while (experiment_time < experiment_duration) {
                // calculates the workload values
                int db_id = random.nextInt(db_id_list.length);

                int signal = random.nextInt(2);
                int variation = random.nextInt(35);
                int sla = 0;
                int size = 0;
                int avg_response_time = 0;
                int response_time = 0;

                Statement statement = connection.createStatement();

                ResultSet resultSet = statement.executeQuery("select sla, size from db where id = " + db_id);
                while (resultSet.next()) {
                    String temp1 = resultSet.getString("sla");
                    if (temp1 != null) {
                        sla = Integer.parseInt(temp1);
                    }
                    String temp2 = resultSet.getString("size");
                    if (temp2 != null) {
                        size = Integer.parseInt(temp2);
                    }
                }
                resultSet.close();
                
                resultSet = statement.executeQuery("select avg(response_time) as avg_response_time from db_workload where db_id = " + db_id);
                while (resultSet.next()) {
                    String temp = resultSet.getString("avg_response_time");
                    if (temp != null) {
                        avg_response_time = (int) Double.parseDouble(temp);
                    }
                }
                resultSet.close();

                switch (signal) {
                    case 0: {
                        response_time = avg_response_time - variation;
                        size = size + 2;
                        if (response_time <= 5) {
                            response_time = 5;
                        }
                        break;
                    }
                    case 1: {
                        response_time = avg_response_time + variation;
                        size = size + 4;
                        while (response_time > sla * 2) {
                            response_time -= variation;
                        }
                        break;
                    }
                }

                String sql = "insert into db_workload (\"time\", db_id, response_time) values (now(), " + db_id + ", " + response_time + ")";
                System.out.println(sql);
                statement.executeUpdate(sql);
                statement.executeUpdate("update db set size = " + size + " where id = " + db_id);
                statement.close();

                experiment_time = System.currentTimeMillis();
            }
            connection.close();
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(CloudMonitorWorkload.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(CloudMonitorWorkload.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
